package org.sg20.graphbuilder;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class GraphBuilderReducer extends Reducer<Text, Text, Text, Text> {

    String PR_init = "1.0";
    Text out_key = new Text();
    Text out_val = new Text();

    @Override
    protected void setup(Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        int count = GraphBuilderMapper.getCharacterSize();
        double prInit = 1/(double)(count);
        //PR_init = String.valueOf(prInit);
    }

    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        String val = "";
        out_key = key;
        for (Text value : values) {
            val += value.toString();
        }
        out_val.set(String.format("%s|%s",PR_init, val));
        /*
        输出为 唐僧 1.0|悟空,八戒
         */
        context.write(key, out_val);
    }
}
